package com.snc.util;

import com.snc.entity.ResidentFormMap;
import com.snc.entity.UserFormMap;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class PasswordHelper {
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private String algorithmName = "md5";
	private int hashIterations = 2;

	public void encryptPassword(UserFormMap userFormMap) {
		String salt=randomNumberGenerator.nextBytes().toHex();
		userFormMap.put("credentialsSalt", salt);
		String newPassword = new SimpleHash(algorithmName, userFormMap.get("password"), ByteSource.Util.bytes(userFormMap.get("accountName")+salt), hashIterations).toHex();
		userFormMap.put("password", newPassword); 
	}

	public void encryptPassword(ResidentFormMap residentFormMap) {
		String salt=randomNumberGenerator.nextBytes().toHex();
		residentFormMap.put("credentialsSalt", salt);
		String newPassword = new SimpleHash(algorithmName, residentFormMap.get("password"), ByteSource.Util.bytes(residentFormMap.get("phone")+salt), hashIterations).toHex();
		residentFormMap.put("password", newPassword);
	}

	public String encryptPassword(String phone,String password,String salt){
		return new SimpleHash(algorithmName, password, ByteSource.Util.bytes(phone+salt), hashIterations).toHex();
	}

	public static void main(String[] args) {
		PasswordHelper passwordHelper = new PasswordHelper();
		UserFormMap userFormMap = new UserFormMap();
		userFormMap.put("password","123456");
		userFormMap.put("accountName","admin");
		passwordHelper.encryptPassword(userFormMap);
		System.out.println(ByteSource.Util.bytes("admin4157c3feef4a6ed91b2c28cf4392f2d1"));
	}
}
